{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# *OPTIONAL* Input Widgets and Geospatial Data Analysis\n",
    "\n",
    "In addition to ipywidgets 7, and pythreejs, this example requires GDAL\n",
    "\n",
    "**GDAL installation:**\n",
    "\n",
    "```bash\n",
    "conda install gdal\n",
    "conda install krb5\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "from ipywidgets import Controller, FloatText, HTML, VBox\n",
    "from pythreejs.install import install\n",
    "from traitlets.traitlets import link, dlink"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Terrain modeling? Let us check out the Grand Canyon"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "from pythreejs import *\n",
    "import numpy as np\n",
    "import gdal as gd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "gc_ds = gd.Open('gc_dem.tif')\n",
    "dem = gc_ds.ReadAsArray()[::20, ::20]\n",
    "gt = gc_ds.GetGeoTransform()\n",
    "\n",
    "z = (dem - np.mean(dem)) / 1000\n",
    "nx, ny = z.shape\n",
    "\n",
    "surf_g = SurfaceGeometry(z=list(z.flat), height_segments=nx - 1, width_segments=ny - 1)\n",
    "surf = Mesh(geometry=surf_g, material=LambertMaterial(map=height_texture(z[::-1], colormap='terrain')), scale=(10, 10, 1))\n",
    "scene = Scene(children=[AmbientLight(color='#777777'),\n",
    "                        surf, \n",
    "                        DirectionalLight(color='white', position=[3, 5, 1], intensity=0.5)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "c = PerspectiveCamera(position=[0, 10, 10], up=[0, 0, 1], \n",
    "                      children=[DirectionalLight(color='white', position=[3, 5, 1], intensity=0.5)],\n",
    "                      aspect=2)\n",
    "width = 950\n",
    "height = 950 / c.aspect\n",
    "c.look_at(c.position, (1, 0, 0))\n",
    "fly_controls = FlyControls(controlling=c)\n",
    "renderer = Renderer(camera=c, scene=scene, width=str(width), height=str(height), controls=[fly_controls])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "c.position = [0, 10, 10]\n",
    "c.look_at(c.position, (1, 0, 0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "5a5e362ae6c44beb8827ef005a24490e",
       "version_major": "2",
       "version_minor": "0"
      },
      "text/plain": [
       "A Jupyter Widget"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "renderer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Adjusting the camera position?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "c.position = [10, 15, 10]\n",
    "c.look_at(c.position, (1, 0, 0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<img src=\"Flow.svg\"></img>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sliders?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "from ipywidgets import FloatSlider, HBox, VBox\n",
    "\n",
    "x_slider, y_slider, z_slider = (FloatSlider(description='x', min=10.0, max=20.0, orientation='vertical'),\n",
    "                                FloatSlider(description='y', min=10.0, max=20.0, orientation='vertical'),\n",
    "                                FloatSlider(description='z', min=10.0, max=20.0, orientation='vertical'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "def update(change):\n",
    "    c.position = [x_slider.value, y_slider.value, z_slider.value]\n",
    "    \n",
    "x_slider.observe(update, names=['value'])\n",
    "y_slider.observe(update, names=['value'])\n",
    "z_slider.observe(update, names=['value'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "7c5f4ef6b75b4b18b2646fce3d357aef",
       "version_major": "2",
       "version_minor": "0"
      },
      "text/plain": [
       "A Jupyter Widget"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "HBox([x_slider, y_slider, z_slider, renderer])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/vnd.jupyter.widget-view+json": {
       "model_id": "ea990d1cc0304c3dbb7edb83757e076a",
       "version_major": "2",
       "version_minor": "0"
      },
      "text/plain": [
       "A Jupyter Widget"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "pad = Controller()\n",
    "pad"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.0"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pad.axes[1].value"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": [
    "factor = 10\n",
    "def affine(constant, factor):\n",
    "    return lambda x: constant + factor * x\n",
    "\n",
    "pad.links = []\n",
    "\n",
    "def setup():\n",
    "    if pad.connected:\n",
    "        pad.links.append(dlink((pad.axes[1], 'value'), (fly_controls, 'pitch'), affine(0.0, factor)))\n",
    "        pad.links.append(dlink((pad.axes[0], 'value'), (fly_controls, 'roll'), affine(0.0, -factor)))\n",
    "        pad.links.append(dlink((pad.axes[3], 'value'), (fly_controls, 'forward_speed'), affine(0.0, 2 * factor)))\n",
    "        pad.links.append(dlink((pad.axes[2], 'value'), (fly_controls, 'yaw'), affine(0.0, factor)))\n",
    "    if not pad.connected:\n",
    "        for l in pad.links:\n",
    "            l.unlink()\n",
    "        pad.links = []\n",
    "\n",
    "pad.observe(setup, names=['connected'])\n",
    "setup()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "jupyter": {
     "outputs_hidden": true
    }
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "ipywidgets": {
   "state": {
    "054a22b61c454721a782a4cd3f3f60cb": {
     "views": []
    },
    "0acb9e7b75894aec98213cfa6646dc78": {
     "views": []
    },
    "127a10d7c2f74b44b0098ecc0fe43909": {
     "views": []
    },
    "1298ac2f0b1a434ea89c38c298363926": {
     "views": []
    },
    "191afb1217014b318fb330d2d45f84a7": {
     "views": []
    },
    "1ae529886b91456abc092c1fb1d7a808": {
     "views": []
    },
    "1c7e21b1d01a4ce4b17a353adc2b44db": {
     "views": []
    },
    "204bb73174a24c01ba5b17abfd9a1111": {
     "views": []
    },
    "216061b3b80b4ee89b58d81a3f3dc50a": {
     "views": []
    },
    "22b37afbddbb4ba3af781f826dd44c89": {
     "views": []
    },
    "2345f2e27970445f8beba076b9b9e202": {
     "views": []
    },
    "250800e283a541148d04b458bbe48033": {
     "views": []
    },
    "28865d3a013b4ce09fe0c43c875795b8": {
     "views": []
    },
    "295e679326bb45d68589717451a15480": {
     "views": []
    },
    "2d13c49f9b88455b8c20e3753953685f": {
     "views": []
    },
    "2d6dc4dab797494bad07fa9365bbd39b": {
     "views": []
    },
    "2eeefb64bfa2469895ad105ff2d65639": {
     "views": []
    },
    "31901164321640f8805e8d232bbd0020": {
     "views": []
    },
    "377632469e074777aa25118ced6fff4c": {
     "views": []
    },
    "38b23d80c68b43afb9dcf5d64f7ff67c": {
     "views": []
    },
    "3dca38ffff3749049716e739455ba32b": {
     "views": []
    },
    "3e23f707f39e4e52a60263a34cbd84c1": {
     "views": []
    },
    "3f9430820052458fb9119eedddb62e8e": {
     "views": []
    },
    "43ef359f633c4f3c8dba3101b295eedd": {
     "views": []
    },
    "446e4e65239949c5a1b1ac0ebb01b72d": {
     "views": []
    },
    "49d2e49386744541ba80dfe1533c3a4b": {
     "views": []
    },
    "4a26b00b26574e24a15a7c6f0afe97a4": {
     "views": []
    },
    "4c660d04cb974e27a729ff8db634eb6c": {
     "views": [
      {
       "cell": {
        "cell_type": "code",
        "execution_count": null,
        "metadata": {
         "collapsed": false,
         "trusted": true
        },
        "outputs": [],
        "source": "import numpy as np\nfrom pythreejs import *\n\nnx, ny= (20, 20)\nxmax = 1\nx = np.linspace(-xmax, xmax, nx)\ny = np.linspace(-xmax, xmax, ny)\nxx, yy = np.meshgrid(x, y)\nz = xx**2-yy**2\n\nsurf_g = SurfaceGeometry(z=list(z[::-1].flat), \n                          width=2 * xmax,\n                          height=2 * xmax,\n                          width_segments=nx-1,\n                          height_segments=ny-1)\n\nsurf = Mesh(geometry=surf_g, material=LambertMaterial(map=height_texture(z[::-1], 'YlGnBu_r')))\nsurfgrid = SurfaceGrid(geometry=surf_g, material=LineBasicMaterial(color='black'))\nhover_point = Mesh(geometry=SphereGeometry(radius=0.05), material=LambertMaterial(color='hotpink'))\nscene = Scene(children=[surf, surfgrid, hover_point, AmbientLight(color='#777777')])\nc = PerspectiveCamera(position=[0,3,3], up=[0,0,1], \n                      children=[DirectionalLight(color='white', position=[3, 5, 1], intensity=0.6)])\nclick_picker = Picker(root=surf, event='dblclick')\nhover_picker = Picker(root=surf, event='mousemove')\nrenderer = Renderer(camera=c, scene = scene, controls=[OrbitControls(controlling=c), click_picker, hover_picker])\n\ndef f(change):\n    value = change['new']\n    print('Clicked on %s' % value)\n    point = Mesh(geometry=SphereGeometry(radius=0.05), \n                 material=LambertMaterial(color='red'),\n                 position=value)\n    scene.children = list(scene.children) + [point]\n\nclick_picker.observe(f, names='point')\n\nlink((hover_point, 'position'), (hover_picker, 'point'))\n\nh = HTML()\ndef g(change):\n    h.value=\"Green point at (%.3f, %.3f, %.3f)\" % tuple(change['new'] )\n    \nhover_picker.observe(g, names='point')\nVBox([h, renderer])"
       },
       "cell_index": 4,
       "root": true
      }
     ]
    },
    "4c6c8a248df74ec89b7a62cea3047089": {
     "views": []
    },
    "4d65afba88f74dd3b56ca9451c979e0a": {
     "views": []
    },
    "51986ccc21e1459d80a2cdbf4f68401e": {
     "views": []
    },
    "5c1d0ace2b8d4b6fb349d4c11f8c871c": {
     "views": []
    },
    "5ddb6968d52f402993c73bf659fa6ce4": {
     "views": []
    },
    "5e7e5f04de98442cbb2a34189bb8e593": {
     "views": []
    },
    "5f8dd35d77e74ef88ac4cf32637b8c23": {
     "views": []
    },
    "5fafff2bed964e3a80edb3f81f90c0fd": {
     "views": []
    },
    "60716ec1e93d43bd904e86fe11cb1572": {
     "views": []
    },
    "6582c30ea33c4bbca0e890049664db80": {
     "views": []
    },
    "663fe15bbf034a0e9305f50cce256a78": {
     "views": []
    },
    "68443dee23924573b4bf212bbef7657f": {
     "views": []
    },
    "6a9401bd30fe477da09c70b9376640b7": {
     "views": []
    },
    "6e5f4e37213a4a559fe6a155bd37667b": {
     "views": []
    },
    "77eabaf265064817a9aaace165be0c6d": {
     "views": []
    },
    "7cfb559dfd33414aa83f762cb8783c1a": {
     "views": []
    },
    "806b3c48c6c047f5aac5783cdb9f75b9": {
     "views": []
    },
    "815e760f585e40bc84d43d9bb2ade9dc": {
     "views": []
    },
    "85e404f114d348cbafdfdc445dbadb0b": {
     "views": []
    },
    "891a92be33ef43f6ad85a120930e7df6": {
     "views": []
    },
    "89b9fd8c16e3474096686947994e1e88": {
     "views": []
    },
    "89c6bf7eb1a44ba9ac6de9edf812ce47": {
     "views": []
    },
    "8c745a0eeb074e0ab6276f84dd935aef": {
     "views": []
    },
    "996d58195edd4647821a2f136b4241d0": {
     "views": []
    },
    "9c0e39dc77b14e0097191c5f8d0f796b": {
     "views": []
    },
    "9d8982f7c0cb40ec9f3c9314dd8b626b": {
     "views": []
    },
    "9dbba2be57284ca6a9991e892aee13f5": {
     "views": []
    },
    "9e4208bf949540e081cef964b60e91d1": {
     "views": []
    },
    "a24583f58c694954b0a28fb6cea74281": {
     "views": []
    },
    "a5f8095305d84bf184e0a757baac2d79": {
     "views": []
    },
    "a7546af6e0dd4d1697d90e58342b5949": {
     "views": []
    },
    "a92f56e3815c4f85a4dcc4f134853363": {
     "views": []
    },
    "a9f166a29f304a9a8cb95deb5917f553": {
     "views": []
    },
    "ac082ec0a5d8487996cf67f042acab3e": {
     "views": []
    },
    "afff6216fc9a4920a287404f12c648bf": {
     "views": []
    },
    "b2d5b82569a8402cbf117573f4a27201": {
     "views": []
    },
    "bd3a76d6d53b46618bf1e621c0bd4d30": {
     "views": []
    },
    "bd6e552e5af84c1d9e87f3a4aaf449f1": {
     "views": [
      {
       "cell": {
        "cell_type": "code",
        "execution_count": null,
        "metadata": {
         "collapsed": false,
         "trusted": true
        },
        "outputs": [],
        "source": "pad = Controller()\npad"
       },
       "cell_index": 2,
       "root": true
      }
     ]
    },
    "c0b3f6197f0e4129a1078893b8291ad0": {
     "views": []
    },
    "c6c18d8c86374a9c8d98c32f3027bb6d": {
     "views": []
    },
    "c76954a22ecd4c4783eb025b0f5afb97": {
     "views": [
      {
       "cell": {
        "cell_type": "code",
        "execution_count": null,
        "metadata": {
         "collapsed": false,
         "trusted": true
        },
        "outputs": [],
        "source": "renderer"
       },
       "cell_index": 10,
       "root": true
      }
     ]
    },
    "c7da67a6cbb740a8babb125e3b0d5ffe": {
     "views": []
    },
    "c89b5b597f1b4c2eb0175b8c5abb43a0": {
     "views": []
    },
    "ccaf5f57308f414a9bfba0ccad123785": {
     "views": []
    },
    "ccdee0949ccb44ea881a6d3f6d9810c3": {
     "views": []
    },
    "d74632113f90457e80fbdd8e1a61be98": {
     "views": []
    },
    "d90af68998344fc7881218809913d427": {
     "views": []
    },
    "d9b104a1b95b4ff18f7d2356bb158e2d": {
     "views": []
    },
    "ddec1d7f3004467c902754a927e97059": {
     "views": []
    },
    "de5a9a6218ae46aa9c8db66dd157c9a3": {
     "views": []
    },
    "e4f63ab7a6994005bd2eefb172c5a1ff": {
     "views": []
    },
    "e5298dc65e73481db14531f12e2092e1": {
     "views": []
    },
    "e7ba7ce4e7e645fd87865c2b840d0dc6": {
     "views": []
    },
    "eab9961a4464463e93c9c7f9e0087f69": {
     "views": []
    },
    "eb70de77fd7a49e79d814a02261951e1": {
     "views": []
    },
    "ef6000edfdd544d1aa28b9c23211cca9": {
     "views": []
    },
    "efd5f6633cf54a6496cc617419e1a69b": {
     "views": []
    },
    "f3e2bd841eb647408d7281a12b4cd7d1": {
     "views": []
    },
    "f43cb8d715104e5194924ddd98f49d0d": {
     "views": []
    },
    "f4dc2b34b141423caefd6b8b068943b5": {
     "views": []
    },
    "f593d458094741f787376032e0b624d4": {
     "views": []
    },
    "fb95621db996440aa1935ed5f62b6989": {
     "views": []
    },
    "fbebbd91ee4f41d78e8ffe8a566aa5ef": {
     "views": []
    },
    "fe54212e7c5444ef9499e983d6bbeb74": {
     "views": []
    },
    "ff7308e1ee8a4ea59246e755497e78ea": {
     "views": []
    },
    "ffcc625980aa4be3b4f4348167231977": {
     "views": []
    }
   },
   "version": "1.0.0"
  },
  "kernelspec": {
   "display_name": "widgets-tutorial",
   "language": "python",
   "name": "widgets-tutorial"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  },
  "widgets": {
   "state": {
    "9b15416c10c345d08bb9adb414356023": {
     "views": [
      {
       "cell_index": 7
      }
     ]
    },
    "f73d038d80e14f13a7bcef45ac525ff7": {
     "views": [
      {
       "cell_index": 14
      }
     ]
    }
   },
   "version": "2.0.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
